﻿using SqlSugar;

namespace Koala.Pro.CodeGenerator.UI.Common
{
    [SugarTable("ColumnNameAlias")]
    public class ColumnNameAliasInfo
    {
        [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
        public int ID { get; set; }

        public string TableName { get; set; }

        public string ColumnName { get; set; }

        public string ColumnAlias { get; set; }

        public string DatabaseType { get; set; }

        public static ColumnNameAliasInfo GetAliasByName(string tableName, string columnName, string databaseType)
        {
            var db = CodeGenerationSettingDbContext.GetDbClient();
            return db.Queryable<ColumnNameAliasInfo>().Single(x => x.TableName == tableName && x.ColumnName == columnName && x.DatabaseType == databaseType);
        }

        public void InsertOrUpdate()
        {
            var db = CodeGenerationSettingDbContext.GetDbClient();
            string empty = string.Empty;
            ColumnNameAliasInfo info = GetAliasByName(TableName, ColumnName, DatabaseType);
            if (info == null)
            {
                db.Insertable(this).ExecuteCommand();
            }
            else
            {
                db.Updateable(this).ExecuteCommand();
            }
        }
    }
}
